layui.use(['table', 'form', 'tree'], function() {
	var table = layui.table;
	var form = layui.form;
	var tree = layui.tree;
	table.render({
		elem: '#test',
		url: 'http://localhost:8080/Online-student/RoleController/getAllRole',
		toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
			,
		where: {
			del: 0
		},
		defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
			title: '提示',
			layEvent: 'LAYTABLE_TIPS',
			icon: 'layui-icon-tips'
		}],
		title: '用户数据表',
		cols: [
			[{
				type: 'checkbox',
				fixed: 'left'
			}, {
				field: 'rid',
				title: '编号',
			}, {
				field: 'rname',
				title: '角色名称',
			}, {
				field: 'description',
				title: '角色功能描述',
			}, {
				fixed: 'right',
				title: '操作',
				toolbar: '#barDemo',
			}]
		],
		page: true,
		parseData: function(res) { //将原始数据解析成 table 组件所规定的数据
			console.log(res);
			return {
				"code": res.status, //解析接口状态
				"msg": res.message, //解析提示文本
				"count": res.data.total, //解析数据长度
				"data": res.data.list //解析数据列表
			};
		}
	});

	//头工具栏事件
	table.on('toolbar(test)', function(obj) {
		var checkStatus = table.checkStatus(obj.config.id);
		switch (obj.event) {
			case 'addData':
				$("#formone")[0].reset();
				layer.open({
					type: 1,
					title: '增加',
					area: '500px',
					offset: ['0%', '20%'],
					btn: ['确认', '取消'],
					content: $("#formone"),
					yes: function(index, layero) {
						$.ajax({
							url: "http://localhost:8080/Online-student/RoleController/addRole",
							type: "get",
							data: {
								rname: $("#rname").val(),
								description: $("#description").val(),
								//del: $("#del").val()
							},
							dataType: "json",
							success: function(result) {
								layer.closeAll();
								layer.msg(result.message);
								$("#formone")[0].reset();
								//执行重载
								table.reload('test');
							}
						})
					},
					btn2: function(index, layero) {}
				});
				break;
			case 'getCheckLength':
				layer.msg(checkStatus.isAll ? '全选' : '未全选');
				break;
		};
	});

	//触发行单击事件 row<单击> rowDouble<双击>
	table.on('rowDouble(test)', function(obj) {
		form.val("formone", obj.data);
		layer.open({
			type: 1,
			area: '500px',
			offset: ['0%', '20%'],
			title: '详情',
			content: $("#formone")
		});
	});
	//监听行工具事件
	table.on('tool(test)', function(obj) {
		var data = obj.data;
		//console.log(obj)
		if (obj.event === 'del') {
			layer.confirm('真的删除行么', function(index) {
				$.ajax({
					url: "http://localhost:8080/Online-student/RoleController/delRole",
					type: "get",
					data: {
						rid: data.rid,
						//del:data.del,
						del : 1
					},
					dataType: "json",
					success: function(result) {
						if (result.status == 0) {
							layer.closeAll();
							layer.msg(result.message);
							//执行重载
							table.reload('test');
						}
					}

				})
			});
		} else if (obj.event === 'edit') {
			form.val("formone", data);
			layer.open({
				type: 1,
				title: '修改',
				area: '500px',
				offset: ['0%', '20%'],
				btn: ['确认', '取消'],
				content: $("#formone"),
				yes: function(index, layero) {
					$.ajax({
						url: "http://localhost:8080/Online-student/RoleController/updateRole",
						type: "get",
						data: {
							rid: $("#rid").val(),
							rname: $("#rname").val(),
							description: $("#description").val(),
							//del: $("#del").val()
						},
						dataType: "json",
						success: function(result) {
							if (result.status == 0) {
								layer.closeAll();
								layer.msg(result.message);
								$("#formone")[0].reset();
								//执行重载
								table.reload('test');
							}
						}
					})
				},
				btn2: function(index, layero) {}
			});
		}else if (obj.event === 'setRole') {
			layer.open({
				type: 1,
				title: '授权',
				area: '500px',
				offset: ['0%', '20%'],
				btn: ['确认', '取消'],
				content: $("#test12"),
				success: function(){
					initTreeMenus(data.rid);
				},
				yes: function(index, layero) {
					/**
					 * 1.获取当前行的 rid
					 * 2.所有选中的 mid
					 */
					var rid = data.rid;
					var checkData = tree.getChecked('demoId1');
					var ids = new Array();
					for (var i = 0; i < checkData.length; i++) {
						ids.push(checkData[i].id);
						var children = checkData[i].children;
						for (var j = 0; j < children.length; j++) {
							ids.push(children[j].id);
						}
					}
					if(ids.length == 0){
						layer.msg("授权失败，不能不设置权限");
						return;
					}
					setTreeMenus(rid,ids);
				}
			});
		}
	});
	function initTreeMenus(rid){
		$.ajax({
			url: 'http://localhost:8080/Online-student/RoleController/getTreeMenus',
			type: 'get',
			async: false,
			data: {
				rid: rid
			},
			dataType: 'json',
			success: function(result) {
				//基本演示
				tree.render({
					elem: '#test12',
					data: result.data,
					showCheckbox: true, //是否显示复选框
					id: 'demoId1',
				});
			}
		})
	}
	/**
	 * 	授权
	 * @param {Object} rid
	 * @param {Object} ids
	 */
	function setTreeMenus(rid,ids){
		$.ajax({
			url: 'http://localhost:8080/Online-student/RoleController/updateRoleMenus',
			type: 'get',
			data: {
				rid: rid,
				ids: ids
			},
			dataType: 'json',
			success: function(result) {
				if(result.status == 0){
					layer.closeAll();
					layer.msg("授权成功");
					//执行重载
					table.reload('test');
				}
			}
		})
	}
});
